package com.qianfeng.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.dto.Page;
import com.qianfeng.entity.Drug;
import com.qianfeng.mapper.DrugMapper;
import com.qianfeng.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DrugServiceImpl implements DrugService {

    @Autowired
    private DrugMapper drugMapper;
    @Override
    public void addDrug(Drug drug) {
        drugMapper.addDrug(drug);
    }

    @Override
    public void updateDrug(Drug drug) {
        drugMapper.updateDrug(drug);
    }

    @Override
    public Page getDrugPage(int pageNo, int pageSize) {
        Page page = new Page();
        PageHelper.startPage(pageNo, pageSize);
        List<Drug> drugList = drugMapper.getDrugList();
        PageInfo<Drug> pageInfo = new PageInfo<>(drugList);
        page.setObj(pageInfo.getList());
        page.setPageCount(pageInfo.getPages());
        page.setHasNext(pageInfo.isHasNextPage());
        System.out.println(pageInfo.isHasNextPage());
        return page;
    }

    @Override
    public Drug getDrugDetailByDrId(Integer drId) {
        return drugMapper.getDrugDetailByDrId(drId);
    }

    @Override
    public void addDrugStore(Integer drId, Integer num) {
        drugMapper.addDrugStore(drId,num);
    }

    @Override
    public List<Drug> getDrugList() {
        return drugMapper.getDrugList();
    }
}
